package com.spzx.product.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.common.security.utils.SecurityUtils;
import com.spzx.product.domain.CategoryBrand;
import com.spzx.product.domain.ProductSpec;
import com.spzx.product.service.ProductSpecService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.Data;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/spec")
@Tag(name = "商品规格参数管理模块")
public class ProductSpecController extends BaseController {

    @Resource
    private ProductSpecService productSpecService;

    @Operation(summary = "条件查询商品规格参数分页列表")
    @GetMapping("/list")
    public TableDataInfo list(ProductSpec productSpec) {
        //此处使用mp实现分页查询
        Page<ProductSpec> page = productSpecService.listPage(productSpec);
        //使用自己再BaseController创建的重载方法 将page转为TableDataInfo对象返回
        return getDataTable(page);
    }

    @Operation(summary = "批量删除商品规格参数")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids) {
        //toAjax 可以根据增删改返回的影响记录的条数，返回成功或失败AjaxResult对象
        return toAjax(productSpecService.removeBatchByIds(ids));
    }

    @Operation(summary = "新增分类单位")
    @PostMapping
    public AjaxResult add(@RequestBody ProductSpec productSpec) {
        //唯一性校验
        productSpecService.checkProductSpecUnique(productSpec);
        productSpec.setCreateBy(SecurityUtils.getUsername());
        return toAjax(productSpecService.save(productSpec));
    }

    //先查询详情
    @Operation(summary = "查询分类单位详情")
    @GetMapping("/{id}")
    public AjaxResult getById(@PathVariable Long id) {
        return success(productSpecService.getById(id));
    }

    //前端对详情进行展示 修改 提交更新请求
    @Operation(summary = "更新分类单位")
    @PutMapping
    public AjaxResult edit(@RequestBody ProductSpec productSpec) {
        //唯一性校验
        productSpecService.checkProductSpecUnique(productSpec);
        productSpec.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(productSpecService.updateById(productSpec));
    }
    @Operation(summary = "根据三级分类id查询规格参数列表")
    @GetMapping("listByC3Id/{c3id}")
    public AjaxResult list(@PathVariable("c3id")Long c3id){

        return success(productSpecService.list(Wrappers.lambdaQuery(ProductSpec.class)
                .eq(ProductSpec::getCategoryId , c3id)));
    }
}
